const url = "http://localhost:3002"
const querystring = {
    parse : function(url){
        let params = {};
        if(url.indexOf("?") >= 0){
            let search = url.substring(url.indexOf("?") + 1);
            let searchObj = search.split("&");
            for(let item of searchObj){
                let itemObj = item.split("=");
                params[itemObj[0]] = itemObj[1];
            }
        }
        return params;
    }
}

const fillFilms = function($element,data){
    let str = data.map((item) => {
        let films = item.films || item;
        return `
            <div class="film">
                <a href="detail.html?id=${films._id}"><img src="${url}/images/${films.images[0]}" alt="" /></a>
                <div class="info">
                    <h4>${films.name}</h4>
                    <a class="button" href="/cinemas.html?filmId=${films._id}">购票</a>
                </div>
            </div>
        `
    }).join("");
    $element.html(str);
}

// 数组去重
const distinctAry = function(ary){
    var newAry = [];
    for(var i = 0;i < ary.length;i++){
        var flag = false;
        for(var j = 0;j < newAry.length;j++){
            if(ary[i] == newAry[j]){
                flag = true;
            }
        }
        if(!flag){
            newAry.push(ary[i]);
        }
    }
    return newAry;
}

Date.prototype.format = function (fmt) {  
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    if (/(E+)/.test(fmt)){
        
        let day = "";
        switch(this.getDay()){
            case 0:day = "周日";break;
            case 1:day = "周一";break;
            case 2:day = "周二";break;
            case 3:day = "周三";break;
            case 4:day = "周四";break;
            case 5:day = "周五";break;
            case 6:day = "周六";break;
        }
        fmt = fmt.replace(RegExp.$1,day);
    }
    return fmt;
}

export {
    querystring,fillFilms,distinctAry,url
}
